<?php


/**
 * 根据请求头user-agent获取手机品牌
 * @param string $agent
 * @return string
 */
function getClientMobileBrand($agent = ''){
    if(preg_match('/iPhone\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'iphone';
    }elseif(preg_match('/SAMSUNG|Galaxy|GT-|SCH-|SM-\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'samsung';
    }elseif(preg_match('/Huawei|Honor|H60-|H30-\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'HUAWEI';
    }elseif(preg_match('/Mi note|mi one\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'xiaomi';
    }elseif(preg_match('/HM NOTE|HM201\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'xiaomi';
    }elseif(preg_match('/Coolpad|8190Q|5910\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = '酷派';
    }elseif(preg_match('/ZTE|X9180|N9180|U9180\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = '中兴';
    }elseif(preg_match('/OPPO|X9007|X907|X909|R831S|R827T|R821T|R811|R2017|PBEM00|PACM00\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'OPPO';
    }elseif(preg_match('/HTC|Desire\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'HTC';
    }elseif(preg_match('/Nubia|NX50|NX40\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = '努比亚';
    }elseif(preg_match('/M045|M032|M355\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'Meizu';
    }elseif(preg_match('/Gionee|GN\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = '金立';
    }elseif(preg_match('/HS-U|HS-E\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = '海信';
    }elseif(preg_match('/Lenove\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'lenovo';
    }elseif(preg_match('/ONEPLUS\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'OnePlus';
    }elseif(preg_match('/vivo\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'vivo';
    }elseif(preg_match('/K-Touch\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = '天语';
    }elseif(preg_match('/DOOV\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = '朵唯';
    }elseif(preg_match('/GFIVE\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = '基伍';
    }elseif(preg_match('/Nokia\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'Nokia';
    }elseif(preg_match('/honor\s([^\s|;]+)/i', strtolower($agent), $regs)) {
        $mobile_brand = 'Honor';
    }elseif(preg_match('/Newman\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'Newman';
    }elseif(preg_match('/Neken\s([^\s|;]+)/i', $agent, $regs)) {
        $mobile_brand = 'Neken';
    }elseif(preg_match('/philips\s([^\s|;]+)/i', strtolower($agent), $regs)) {
        $mobile_brand = 'Philips';
    }elseif(preg_match('/sony\s([^\s|;]+)/i', strtolower($agent), $regs)) {
        $mobile_brand = 'SONY';
    }elseif(preg_match('/hoswn\s([^\s|;]+)/i', strtolower($agent), $regs)) {
        $mobile_brand = 'Hoswn';
    }else{
        $mobile_brand = '';
    }
    return $mobile_brand;
}

/**
 * 图表时间范围处理，按月/天返回时间段数组
 * @param int $start 开始时间（时间戳）
 * @param int $end 结束时间（时间戳）
 * @return array
 */
function getTimeArray($param, $start = null, $end = null) {
    if ($start == null || $end == null) {
        //$param = request()->param();
        switch ($param['type']) {
            // 本年
            case 'year':
                $start = strtotime(date('Y-01-01'));
                $end = strtotime('+1 year', $start) - 1;
                break;
            // 去年
            case 'lastYear':
                $start = strtotime(date(date('Y') - 1 . '-01-01'));
                $end = strtotime('+1 year', $start) - 1;
                break;
            // 本季度、上季度
            case 'quarter':
            case 'lastQuarter':
                $t = intval((date('m') - 1) / 3);
                $start_y = ($t * 3) + 1;
                $start = strtotime(date("Y-{$start_y}-01"));
                if ($param['type'] == 'lastQuarter') { // 上季度
                    $start = strtotime('-3 month', $start);
                }
                $end = strtotime('+3 month', $start) - 1;
                break;
            // 本月、上月
            case 'month':
            case 'lastMonth':
                $start = strtotime(date('Y-m-01'));
                if ($param['type'] == 'lastMonth') {
                    $start = strtotime('-1 month', $start);
                }
                $end = strtotime('+1 month', $start) - 1;
                break;
            // 本周、上周
            case 'week':
            case 'lastWeek':
                $start = strtotime('-' . (date('w') - 1) . 'day', strtotime(date('Y-m-d')));
                if ($param['type'] == 'lastWeek') {
                    $start = strtotime('-7 day', $start);
                }
                $end = strtotime('+7 day', $start) - 1;
                break;
            // 今天、昨天
            case 'tody':
            case 'yesterday':
                $start = strtotime(date('Y-m-d'));
                if ($param['type'] == 'yesterday') {
                    $start = strtotime('-1 day', $start);
                }
                $end = strtotime('+1 day', $start) - 1;
                break;
            default:
                if ($param['start_time'] && $param['end_time']) {
                    $start = $param['start_time'];
                    $end = $param['end_time'].' 23:59:59';
                } else {
                    $start = strtotime(date("Y-m-d"),time());
                    $end = strtotime(date('Y-m-d H:i:s'));
                }
                break;
        }
    }

    $between = [$start, $end];
    $list = [];
    $len = ($end - $start) / 86400;
    // 大于30天 按月统计、小于按天统计
    if ($len > 30) {
        $time_format = '%Y-%m';
        while (true) {
            $start = strtotime(date('Y-m-01', $start));
            $item = [];
            $item['type'] = date('Y-m', $start);
            $item['start_time'] = $start;
            $item['end_time'] = strtotime('+1 month', $start) - 1;
            $list[] = $item;
            if ($item['end_time'] >= $end) break;
            $start = $item['end_time'] + 1;
        }
    } else {
        $time_format = '%Y-%m-%d';
        while (true) {
            $item = [];
            $item['type'] = date('Y-m-d', $start);
            $item['start_time'] = $start;
            $item['end_time'] = strtotime('+1 day', $start) - 1;
            $list[] = $item;
            if ($item['end_time'] >= $end) break;
            $start = $item['end_time'] + 1;
        }
    }

    return [
        'list' => $list, // 时间段列表
        'time_format' => $time_format, // 时间格式 mysql 格式化时间戳
        'between' => $between // 开始结束时间
    ];
}

/**
 * 获取毫秒级时间戳
 * @return float
 */
function get_millisecond()
{
    list($usec, $sec) = explode(" ", microtime());
    $msec=round($usec*1000);
    return $msec;
}

/**
 * 两个坐标点直线距离 km
 * @param $lat1
 * @param $lng1
 * @param $lat2
 * @param $lng2
 * @return string
 */
function location2distance($lat1, $lng1, $lat2, $lng2)
{
    // 将角度转为狐度
    $radLat1 = deg2rad($lat1); // deg2rad()函数将角度转换为弧度
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6371;
    return bcadd($s, 0, 2);
}

/**
 * 阿拉伯数字转中文
 * @param $num
 * @return mixed|string
 */
function numberToWord($num)
{
    $chiNum = array(
        '零',
        '一',
        '二',
        '三',
        '四',
        '五',
        '六',
        '七',
        '八',
        '九'
    );
    $chiUni = array(
        '',
        '十',
        '百',
        '千',
        '万',
        '亿',
        '十',
        '百',
        '千'
    );
    $chiStr = '';
    $num_str = ( string )$num;
    $count = strlen($num_str);
    $last_flag = true;
    $zero_flag = true;
    $temp_num = null;
    $chiStr = '';
    if ($count == 2) {
        $temp_num = $num_str [0];
        $chiStr = $temp_num == 1 ? $chiUni [1] : $chiNum [$temp_num] . $chiUni [1];
        $temp_num = $num_str [1];
        $chiStr .= $temp_num == 0 ? '' : $chiNum [$temp_num];
    } else if ($count > 2) {
        $index = 0;
        for ($i = $count - 1; $i >= 0; $i--) {
            $temp_num = $num_str [$i];
            if ($temp_num == 0) {
                if (!$zero_flag && !$last_flag) {
                    $chiStr = $chiNum [$temp_num] . $chiStr;
                    $last_flag = true;
                }
            } else {
                $chiStr = $chiNum [$temp_num] . $chiUni [$index % 9] . $chiStr;
                $zero_flag = false;
                $last_flag = false;
            }
            $index++;
        }
    } else {
        $chiStr = $chiNum [$num_str [0]];
    }
    return $chiStr;
}



?>
